<?php
namespace app\api\command;

use app\common\model\Gift;
use app\common\model\GiftRecord;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Log;

class WeekUser extends Command
{
    protected function configure()
    {
        $this->setName('weekUser')->setDescription('update weekUser handle 更新本周周星用户处理');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $this->doCron();
    }

    /**
     * 更新本周周星用户  每分钟执行一次
     */
    public function doCron(){

        $cache = cache('command'.__CLASS__);
        if ($cache){
            echo '任务正在执行中<br />';return false;
        }else{
            cache('command'.__CLASS__,true,1800);
        }

        $gift_ids = Gift::where(['is_naming'=>1,'status' =>1])->column('gift_id');
        $map['a.create_time'] = ['between time', [Carbon::now()->startOfWeek()->timestamp, time()]];
        $map['a.status'] = 1;

        foreach ($gift_ids as $gift_id){
            $map['a.gift_id'] = $gift_id;
            $data['naming'] = GiftRecord::weekUser($map)->nick_name ?? '';
            Gift::where('gift_id', $gift_id)->update($data);
        }

        cache('command'.__CLASS__,null);
    }
}